package it.polimi.TravelDream.web.beans;

import java.util.ArrayList;
import java.util.List;

import it.polimi.TravelDream.ejb.dtos.FlightDTO;
import it.polimi.TravelDream.ejb.eaos.FlightSearch;

import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

@ManagedBean(name = "FlightSearch")
@SessionScoped
public class FlightMgrBean {

	@EJB
	private FlightSearch flightSearch;
	
	private List <FlightDTO> currentList;
	private FlightDTO current;

	private int index;
	
	public FlightMgrBean(){
		this.currentList = new ArrayList<FlightDTO>();
		this.current = new FlightDTO();
	}
	
	public String search(){
		flightSearch.search();
		this.currentList = flightSearch.getResultList();
		return "search flight results?faces-redirect=true";
	}

	public List<FlightDTO> getCurrentList() {
		return currentList;
	}

	public void setCurrentList(List<FlightDTO> currentList) {
		this.currentList = currentList;
	}

	public FlightSearch getFlightSearch() {
		return flightSearch;
	}

	public void setFlightSearch(FlightSearch flightSearch) {
		this.flightSearch = flightSearch;
	}
	
	public FlightDTO getCurrent() {
		return current;
	}

	public void setCurrent(FlightDTO current) {
		this.current = current;
	}
	
	public String update() {
		flightSearch.update(current, index);
		return "search flight results?faces-redirect=true";
	}
	
	public String goToUpdate(FlightDTO flight) {
		this.current = flight;
		this.index = currentList.indexOf(flight);
		return "update flight?faces-redirect=true";
	}
	
	public String searchAll() {
		flightSearch.searchAll();
		this.currentList = flightSearch.getResultList();
		return "search flight results?faces-redirect=true";
	}
}
